﻿using System;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Contrib;
using Fitbit.Models;
using Windows.Foundation;

namespace Fitbit
{
    public class Authenticator
    {
        private string ConsumerKey;
        private string ConsumerSecret;
        private string RequestTokenUrl;
        private string AccessTokenUrl;
        private string AuthorizeUrl;
        private string RequestToken;
        private string RequestTokenSecret;

        public Authenticator(string ConsumerKey, string ConsumerSecret, string RequestTokenUrl, string AccessTokenUrl, string AuthorizeUrl)
        {
            this.ConsumerKey = ConsumerKey;
            this.ConsumerSecret = ConsumerSecret;
            this.RequestTokenUrl = RequestTokenUrl;
            this.AccessTokenUrl = AccessTokenUrl;
            this.AuthorizeUrl = AuthorizeUrl;
        }

   
        /// <summary>
        /// For Desktop authentication. Your code should direct the user to the FitBit website to get
        /// Their pin, they can then enter it here.
        /// </summary>
        /// <param name="pin"></param>
        /// <returns></returns>
        public AuthCredential GetAuthCredentialFromPin(string pin)
        {
            const string baseUrl = "https://api.fitbit.com";
        
            var client = new RestClient(baseUrl);
            var request = new RestRequest("oauth/access_token", Method.POST);
            client.Authenticator = OAuth1Authenticator.ForAccessToken(ConsumerKey, ConsumerSecret, RequestToken, RequestTokenSecret,pin);
            
            var response = client.Execute(request);
            var qs = new WwwFormUrlDecoder(response.Content);
            return new AuthCredential()
            {
                AuthToken = qs.GetFirstValueByName("oauth_token"),
                AuthTokenSecret = qs.GetFirstValueByName("oauth_token_secret"),
                UserId = qs.GetFirstValueByName("encoded_user_id")
            };
        }

        public string GetAuthUrlToken()
        {
            const string baseUrl = "https://api.fitbit.com";
            var client = new RestClient(baseUrl);
            client.Authenticator = OAuth1Authenticator.ForRequestToken(this.ConsumerKey, this.ConsumerSecret);

            var request = new RestRequest("oauth/request_token", Method.POST);
            IRestResponse response = client.Execute(request);
            var qs = new WwwFormUrlDecoder(response.Content);

            //var qs = HttpUtility.ParseQueryString(response.Content);
            RequestToken = qs.GetFirstValueByName("oauth_token");
         
            RequestTokenSecret = qs.GetFirstValueByName("oauth_token_secret");

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
                throw new Exception("Request Token Step Failed");

          
            request = new RestRequest("oauth/logout_and_authorize"); //this url will force the user to type in username and password
    
            request.AddParameter("oauth_token", RequestToken);
            var url = client.BuildUri(request).ToString();

            return url;
        }


    }
}
